home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Use NUM and MAX Constants

For arrays with lower bound 1, define the upper bound in terms of the number of items in the array. For arrays with lower bound 0, define the upper bound to be the maximum index in the array instead. For example, the following code defines a one-based array containing 100 items.

Public Const NUM_EMPLOYEES = 100
Public Employees(1 To NUM_EMPLOYEES) As EmployeeRecord
        :
    For i = 1 To NUM_EMPLOYEES
        :
    Next i

The following code defines a 0-based array with 100 employees.

Public Const MAX_EMPLOYEE = 99
Public Employees(0 To MAX_EMPLOYEE) As EmployeeRecord
        :
    For i = 0 To MAX_EMPLOYEE
        :
    Next i

If you define the upper bound for a 0-based array in terms of the number of items, For loops and other code that accesses the array are more confusing. Keeping track of which item is the last in the array is just one more thing that can cause confusion.

‘ This version is more confusing.
Public Const NUM_EMPLOYEES = 100
Public Employees(0 To NUM_EMPLOYEES - 1) As EmployeeRecord
        :
    For i = 0 To NUM_EMPLOYEES - 1
        :
    Next i

If for some reason you will frequently need to know the number of items in the array, create separate constants for the largest index and the number of items. Define one constant in terms of the other so you can change both by changing only the first.

Public Const NUM_EMPLOYEES = 100
Public Const MAX_EMPLOYEE = NUM_EMPLOYEES - 1
Public Employees(0 To MAX_EMPLOYEE) As EmployeeRecord
        :
    For i = 0 To MAX_EMPLOYEE
        :
    Next i
    MsgBox “Processed” & Str$(NUM_EMPLOYEES) & “ processed.”

Use LBound and UBound

When the program needs to know an array’s bounds, it should use the same constants that it used to dimension the array. Even better, it can use Visual Basic’s LBound and UBound functions to learn the array’s bounds. Now constants are unnecessary because the magic numbers appear only where the array is allocated.

‘ We need 8 colors with indexes 0 through 7.
Public BackgroundColors(0 To 7) As OLE_COLOR

‘ In another module:
Private Sub InitializeColors()
Dim i As Integer

    ‘ Fill in the BackgroundColors array’s entries.
    For i = LBound(BackgroundColors) To UBound(BackgroundColors)
        BackgroundColors(i) = QBColor(i)
    Next i
End Sub

One time this does not work is if you need to declare several arrays that all have the same bounds. Array declarations require constant values for the array bounds, so they cannot use LBound and UBound. In this case, you can combine the two previous strategies. Use private constants to define the array bounds where the arrays are declared. Use LBound and UBound to find the array bounds in other places.

‘ We need 8 colors with indexes 0 through 7.
Private Const MIN_COLOR = 0
Private Const MAX_COLOR = 7
Public BackgroundColors(MIN_COLOR To MAX_COLOR) As OLE_COLOR
Public ForegroundColors(MIN_COLOR To MAX_COLOR) As OLE_COLOR

‘ In another module:
Private Sub InitializeColors()
Dim i As Integer

    ‘ Fill in the BackgroundColors array’s entries.
    For i = LBound(BackgroundColors) To UBound(BackgroundColors)
        BackgroundColors(i) = QBColor(i)
    Next i
End Sub

It is a little known fact that control arrays have LBound and UBound properties you can use to find the minimum and maximum Index property values for the controls in the array. For example, the following code blanks the TextBoxes in the txtNames control array.

Dim i As Integer

     For i = txtNames.LBound To txtNames.UBound
         txtNames(i).Text =“”
     Next i

Use the control array LBound and UBound properties whenever you need to create variable arrays that should have the same dimensions as a control array.

Note that control arrays are not guaranteed to hold a control with every index between the LBound and UBound values. For example, txtNames might have controls with indexes 0, 1, and 3. In that case, the program must protect itself against trying to access txtNames(2).

Give Related Values Related Names

Give related constants and enumerated values the same prefix so it is obvious that they are related.

For example, suppose the RotatingPicture ActiveX control rotates images. It uses numeric codes to indicate the direction of rotation. The following code defines rotation direction values using the prefix rotdir so it is obvious that they are related.

Public Enum rotpic_RotationDirection
 rotdir_Clockwise
 rotdir_CounterClockwise
 rotdir_None
End Enum

The RotatingPicture control might use the following property procedures to allow a program to get and set the rotation direction.

‘ The control’s private, internal value.
Private mRotationDirection As rotpic_RotationDirection

‘ Return the current rotation direction.
Public Property Get RotationDirection() As rotpic_RotationDirection)
    RotationDirection = mRotationDirection
End Property

‘ Save a new rotation direction.
Public Property Let RotationDirection( _
       new_value As rotpic_RotationDirection)

       mRotationDirection = new_value
End Property

Add and Remove Values with Care

If the program uses a group of related constants or enumerated values in an If or Select statement, be careful when you add or remove values. For example, if you add a new value, it will not be included in Select statements that use the other values until you add it there as well.

The following code defines three filter constants. It then uses the value of the Filter variable to decide which kind of filter to build.

Private Const filter_PREWITT = 1
Private Const filter_HIGH_PASS = 2
Private Const filter_LOW_PASS = 3
    :
    Select Case Filter
        Case filter_PREWITT   ‘ Build a Prewitt filter.
            :
        Case filter_HIGH_PASS ‘ Build a high pass filter.
            :
        Case filter_LOW_PASS  ‘ Build a low pass filter.
            :
    End Select

If you later decide to add a new filter type, creating the new constant filter_LAPLACIAN is not enough. The Select statement does not include the new constant so it will fail if the program selects the new filter type. It will not find the new value in its list of cases so it will not build any filter.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.